<%-- 
    Document   : pmPage
    Created on : Jun 30, 2012, 10:23:45 AM
    Author     : ThanhTai
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<style type="text/css">
    @import url("css/general.css");
</style>

<html>
    <head>
        <script type="text/javascript">
            function schange(form){
                var dayList = new Array(
                new Option("1","01"),new Option("2","02"),new Option("3","03"),
                new Option("4","04"),new Option("5","05"),new Option("6","06"),new Option("7","07"),
                new Option("8","08"),new Option("9","09"),new Option("10","10"),new Option("11","11"),
                new Option("12","12"),new Option("13","13"),new Option("14","14"),new Option("15","15"),
                new Option("16","16"),new Option("17","17"),new Option("18","18"),new Option("19","19"),
                new Option("20","20"),new Option("21","21"),new Option("22","22"),new Option("23","23"),
                new Option("24","24"),new Option("25","25"),new Option("26","26"),new Option("27","27"),new Option("28","28"));

                var yId=form.syearId;
                var chosenoption=yId.options[yId.selectedIndex]
                var y = chosenoption.value;

                var mId=form.smonthId;
                var chosenoption1=mId.options[mId.selectedIndex]
                var m = chosenoption1.value;

                if (m=="1" || m=="3"|| m=="5"|| m=="7"|| m=="8"|| m=="10"|| m=="12"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                    dayList.push(new Option("31","31"));
                } else if (m=="4"|| m=="6"|| m=="9"|| m=="11"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                } else if (y%4!=0 && m=="2"){
                    dayList.push(new Option("29","29"));
                }
                var myselect=form.sdayId
                while (myselect.length>0){
                    myselect.remove(myselect.length-1);
                }
                //add day
                for (var i = 0; i< dayList.length; i++){
                    try{
                        myselect.add(dayList[i], null);
                    }
                    catch(e){
                        myselect.add(dayList[i]);
                    }
                }
            }

            function echange(form){
                var dayList = new Array(
                new Option("1","01"),new Option("2","02"),new Option("3","03"),
                new Option("4","04"),new Option("5","05"),new Option("6","06"),new Option("7","07"),
                new Option("8","08"),new Option("9","09"),new Option("10","10"),new Option("11","11"),
                new Option("12","12"),new Option("13","13"),new Option("14","14"),new Option("15","15"),
                new Option("16","16"),new Option("17","17"),new Option("18","18"),new Option("19","19"),
                new Option("20","20"),new Option("21","21"),new Option("22","22"),new Option("23","23"),
                new Option("24","24"),new Option("25","25"),new Option("26","26"),new Option("27","27"),new Option("28","28"));

                var yId=form.eyearId;
                var chosenoption=yId.options[yId.selectedIndex]
                var y = chosenoption.value;

                var mId=form.emonthId;
                var chosenoption12 = mId.options[mId.selectedIndex]
                var m = chosenoption12.value;

                if (m=="1" || m=="3"|| m=="5"|| m=="7"|| m=="8"|| m=="10"|| m=="12"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                    dayList.push(new Option("31","31"));
                } else if (m=="4"|| m=="6"|| m=="9"|| m=="11"){
                    dayList.push(new Option("29","29"));
                    dayList.push(new Option("30","30"));
                } else if (y%4!=0 && m=="2"){
                    dayList.push(new Option("29","29"));
                }
                var myselect=form.edayId;
                while (myselect.length>0){
                    myselect.remove(myselect.length-1);
                }
                //add day
                for (var i = 0; i< dayList.length; i++){
                    try{
                        myselect.add(dayList[i], null);
                    }
                    catch(e){
                        myselect.add(dayList[i]);
                    }
                }
            }

            function updateProjects(){
                var validate1 = true;
                var formList = document.forms("projectForm");
                var string="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
                string=string+"<projects>";
                for (var i=1; i<formList.length; i++){
                    string=string+"<project>";
                    string=string+"<projectID>";
                    var projectID = formList[i].projectID.value;
                    string=string+projectID;
                    string=string+"</projectID>";

                    string=string+"<projectName>";
                    var projectName = formList[i].projectName.value;
                    string=string+projectName;
                    string=string+"</projectName>";

                    string=string+"<projectManager>";
                    var projectManager = formList[i].projectManager.value;
                    string=string+projectManager;
                    string=string+"</projectManager>";

                    string=string+"<description>";
                    var description= formList[i].description.value;
                    string=string+description;
                    string=string+"</description>";
                    if (description == "")
                    {
                        window.alert("Description NOT Empty!");
                        validate1 = false;
                        break;
                    }

                    string=string+"<numOfMinestone>";
                    var numOfMinestone= formList[i].numOfMinestone.value;
                    string=string+numOfMinestone;
                    string=string+"</numOfMinestone>";

                    string=string+"<startDay>";
                    string=string+"<year>";
                    var sdyear = formList[i].syearId.value;
                    string=string+sdyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var sdmonth= formList[i].smonthId.value;
                    string=string+sdmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var sdday= formList[i].sdayId.value;
                    string=string+sdday;
                    string=string+"</day>";
                    string=string+"</startDay>";

                    string=string+"<endDay>";
                    string=string+"<year>";
                    var edyear= formList[i].eyearId.value;
                    string=string+edyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var edmonth= formList[i].emonthId.value;
                    string=string+edmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var edday= formList[i].edayId.value;
                    string=string+edday;
                    string=string+"</day>";
                    string=string+"</endDay>";

                    string=string+"<status>";
                    var status=formList[i].status.value;
                    string=string+status;
                    string=string+"</status>";

                    string=string+"</project>";

                    if (Number(sdyear) > Number(edyear))
                    {
                        validate1 = false;
                        window.alert("Error: Project '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    } else if (Number(sdyear) == Number(edyear) && Number(sdmonth) > Number(edmonth))
                    {
                        validate1 = false;
                        window.alert("Error: Project '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }else if (Number(sdyear) == Number(edyear) && Number(sdmonth) == Number(edmonth) && Number(sdday) > Number(edday))
                    {
                        validate1 = false;
                        window.alert("Error: Project '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }
                }
                if (validate1)
                {
                    alert('no ajax support');
                    string=string+"</projects>";
                    if(window.ActiveXObject)//IE
                        xml_req = new ActiveXObject("Microsoft.XMLHTTP");
                    else if(window.XMLHttpRequest)
                        xml_req = new XMLHttpRequest();
                    else alert('no ajax support');

                    xml_req = new XMLHttpRequest();
                    xml_req.open("POST", "UpdateProjects", true);
                    xml_req.setRequestHeader('Content-Type','text/xml');
                    xml_req.send(string);
                    xml_req.onreadystatechange = handleUpdateProjectsResponse;
                }
            }

            function handleUpdateProjectsResponse() {
                if (xml_req.readyState == 4) {
                    if (xml_req.status == 200) {
                        window.alert("Update Project table successfull!!");
                    }else
                        window.alert("Update Project table NOT successfull!!");
                }else {
                }
            }

            function updateMinestones(){
                var validate = true;
                var formList = document.forms("minestoneForm");
                var string="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
                string=string+"<minestones>";
                for (var i=1; i<formList.length; i++){
                    string=string+"<minestone>";
                    var j = i+1;
                    string=string+"<minestoneID>";
                    var minestoneID = formList[i].minestoneID.value;
                    string=string+minestoneID;
                    string=string+"</minestoneID>";

                    string=string+"<minestoneName>";
                    var minestoneName = formList[i].minestoneName.value;
                    string=string+minestoneName;
                    string=string+"</minestoneName>";

                    string=string+"<projectID>";
                    var projectID = formList[i].projectID.value;
                    string=string+projectID;
                    string=string+"</projectID>";

                    string=string+"<description>";
                    var description= formList[i].description.value;
                    string=string+description;
                    string=string+"</description>";
                    if (description == "")
                    {
                        window.alert("Error: Minestone '" + minestoneName + "has Description HAVE TO NOT Empty!");
                        validate = false;
                        break;
                    }

                    string=string+"<startDay>";
                    string=string+"<year>";
                    var sdyear = formList[i].syearId.value;
                    string=string+sdyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var sdmonth= formList[i].smonthId.value;
                    string=string+sdmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var sdday= formList[i].sdayId.value;
                    string=string+sdday;
                    string=string+"</day>";
                    string=string+"</startDay>";

                    string=string+"<endDay>";
                    string=string+"<year>";
                    var edyear= formList[i].eyearId.value;
                    string=string+edyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var edmonth= formList[i].emonthId.value;
                    string=string+edmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var edday= formList[i].edayId.value;
                    string=string+edday;
                    string=string+"</day>";
                    string=string+"</endDay>";

                    string=string+"<status>";
                    var status=formList[i].status.value;
                    string=string+status;
                    string=string+"</status>";

                    string=string+"</minestone>";

                    if (Number(sdyear) > Number(edyear))
                    {
                        validate = false;
                        window.alert("Error: Minestone '" + minestoneName +"' have Start Day BIGGER End Day !");
                    } else if (Number(sdyear) == Number(edyear) && Number(sdmonth) > Number(edmonth))
                    {
                        validate = false;
                        window.alert("Error: Minestone '" + minestoneName +"' have Start Day BIGGER End Day !");
                    }else if (Number(sdyear) == Number(edyear) && Number(sdmonth) == Number(edmonth) && Number(sdday) > Number(edday))
                    {
                        validate = false;
                        window.alert("Error: Minestone '" + minestoneName +"' have Start Day BIGGER End Day !");
                    }
                }
                if (validate)
                {
                    string=string+"</minestones>";
                    if(window.ActiveXObject)//IE
                        xml_req = new ActiveXObject("Microsoft.XMLHTTP");
                    else if(window.XMLHttpRequest)
                        xml_req = new XMLHttpRequest();
                    else alert('no ajax support');

                    xml_req = new XMLHttpRequest();
                    xml_req.open("POST", "UpdateMinestones", true);
                    xml_req.setRequestHeader('Content-Type','text/xml');
                    xml_req.send(string);
                    xml_req.onreadystatechange = handleUpdateMinestonesResponse;
                }
            }

            function handleUpdateMinestonesResponse() {
                if (xml_req.readyState == 4) {
                    if (xml_req.status == 200) {
                        window.alert("Update Minestone table successfull!!");
                    }else
                        window.alert("Update Minestone table NOT successfull!!");
                }else {
                }
            }

            function updateTasks(){
                var validate = true;
                var formList = document.forms("TaskForm");
                formList.length;
                var string="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
                string=string+"<tasks>";
                for (var i=1; i<formList.length; i++){
                    string=string+"<task>";
                    string=string+"<taskID>";
                    var taskID = formList[i].taskID.value;
                    
                    string=string+taskID;
                    string=string+"</taskID>";

                    string=string+"<taskName>";
                    var taskName = formList[i].taskName.value;
                    string=string+taskName;
                    string=string+"</taskName>";

                    if (taskName == "")
                    {
                        window.alert("Task Name NOT Empty!");
                        validate = false;
                        break;
                    }

                    string=string+"<minestoneID>";
                    var minestoneID = formList[i].minestoneID.value;
                    string=string+minestoneID;
                    string=string+"</minestoneID>";

                    string=string+"<username>";
                    var username =formList[i].username.value;
                    string=string+username;
                    string=string+"</username>";

                    string=string+"<description>";
                    var description= formList[i].description.value;
                    string=string+description;
                    string=string+"</description>";
                    if (description == "")
                    {
                        window.alert("Description NOT Empty!");
                        validate = false;
                        break;
                    }
                    string=string+"<startDay>";
                    string=string+"<year>";
                    var sdyear = formList[i].syearId.value;
                    string=string+sdyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var sdmonth= formList[i].smonthId.value;
                    string=string+sdmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var sdday= formList[i].sdayId.value;
                    string=string+sdday;
                    string=string+"</day>";
                    string=string+"</startDay>";

                    string=string+"<endDay>";
                    string=string+"<year>";
                    var edyear= formList[i].eyearId.value;
                    string=string+edyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var edmonth= formList[i].emonthId.value;
                    string=string+edmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var edday= formList[i].edayId.value;
                    string=string+edday;
                    string=string+"</day>";
                    string=string+"</endDay>";

                    string=string+"<status>";
                    var status=formList[i].status.value;
                    string=string+status;
                    string=string+"</status>";

                    string=string+"</task>";

                    if (Number(sdyear) > Number(edyear))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + taskName +"' have Start Day BIGGER End Day !");
                        break;
                    } else if (Number(sdyear) == Number(edyear) && Number(sdmonth) > Number(edmonth))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }else if (Number(sdyear) == Number(edyear) && Number(sdmonth) == Number(edmonth) && Number(sdday) > Number(edday))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }
                }
                if (validate)
                {
                    string=string+"</tasks>";
                    if(window.ActiveXObject)//IE
                        xml_req = new ActiveXObject("Microsoft.XMLHTTP");
                    else if(window.XMLHttpRequest)
                        xml_req = new XMLHttpRequest();
                    else alert('no ajax support');

                    xml_req = new XMLHttpRequest();
                    xml_req.open("POST", "UpdateTasks", true);
                    xml_req.setRequestHeader('Content-Type','text/xml');
                    xml_req.send(string);
                    xml_req.onreadystatechange = handleUpdateTasksResponse;
                }
            }

            function handleUpdateTasksResponse() {
                if (xml_req.readyState == 4) {
                    if (xml_req.status == 200) {
                        window.alert("Update Task table successfull!!");
                    }else
                        window.alert("Update Task table NOT successfull!!");
                }else {
                }
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Project Manager Main Page</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div id="user">
                    ${sessionScope.FULLNAME}, <a href="Controller?action=logout">Log out</a>
                </div>
            </div>

            <div id="body">
                <div id="title">
                    <h2 align="center">Project Manager Main Page</h2>
                </div>
                <div id="content">
                    <div id="left">
                        <table id="Function Reference:" width="200px">
                            <tr>
                                <th>Main Functions:</th>
                            </tr>
                            <tr>
                                <td><a href="Controller?action=MainPage">Update Projects Information</a></td>
                            </tr>
                            <tr>
                                <td><a href="Controller?action=Assign Task to staff">Assign Task to staff</a></td>
                            </tr>
                            <tr>
                                <td><a href="Controller?action=Add Members of Project">Add Members of Project</a></td>
                            </tr>
                        </table>
                    </div>
                    <div id="right">
                        <div id="titleRight">
                            <h2 align="center">Update Project Information</h2>
                        </div>
                        <div id="contenRight">
                            <h3 align="left">Update Projects
                            </h3>
                            <c:import url="xml/Projects.xml" var="xmldoc"/>
                            <c:import url="xsl/Projects.xsl" var="xsldoc"/>
                            <x:transform xml="${xmldoc}" xslt="${xsldoc}">
                            </x:transform>
                            <input align="middle" type="button" name="Update Projects" value="Update Projects" onclick="updateProjects();" />
                            <c:set var="projectName" value="${sessionScope.PROJECTNAME}"/>
                            <c:if test="${not empty projectName}">
                                <h3>Update Minestones of Project: ${projectName}</h3>
                                <c:import url="xml/Minestones.xml" var="xmldoc1"/>
                                <c:import url="xsl/Minestones.xsl" var="xsldoc1"/>
                                <x:transform xml="${xmldoc1}" xslt="${xsldoc1}">
                                </x:transform>
                                <input align="middle" type="button" name="Update Minestones" value="Update Projects" onclick="updateMinestones();" />
                                <c:set var="minestoneName" value="${sessionScope.MINESTONENAME}"/>
                                <c:if test="${not empty minestoneName}">
                                    <h3>Update Tasks of Minestone: ${minestoneName}</h3><br/>
                                    <form action="Controller" method="POST">
                                        <table id="tableA" border="1">
                                            <thead>
                                                <tr>
                                                    <th>Task Name</th>
                                                    <th>Start Day</th>
                                                    <th>End Day</th>
                                                    <th>Status</th>
                                                </tr>
                                                <tr>
                                                    <th>
                                                        <input type="text" name="txtTaskNameSearch" value=""/>
                                                    </th>
                                                    <th>
                                                        <select name="SDyearSearch" id="syearId" size="1" onchange="schange(this.form);">
                                                            <option value="2011" selected>2011</option>
                                                            <option value="2012">2012</option>
                                                            <option value="2013">2013</option>
                                                            <option value="2014">2014</option>
                                                            <option value="2015">2015</option>
                                                            <option value="2016">2016</option>
                                                            <option value="2017">2017</option>
                                                            <option value="2018">2018</option>
                                                            <option value="2019">2019</option>
                                                            <option value="2020">2020</option>
                                                            <option value="2021">2021</option>
                                                            <option value="2022">2022</option>
                                                        </select>
                                                        <select name="SDmonthSearch" id="smonthId" size="1" onchange="schange(this.form);">
                                                            <option value="1">January</option>
                                                            <option value="2">February</option>
                                                            <option value="3">March</option>
                                                            <option value="4">April</option>
                                                            <option value="5">May</option>
                                                            <option value="6">June</option>
                                                            <option value="7">July</option>
                                                            <option value="8">August</option>
                                                            <option value="9">September</option>
                                                            <option value="10">October</option>
                                                            <option value="11">November</option>
                                                            <option value="12">December</option>
                                                        </select>
                                                        <select name="SDdaySearch" id="sdayId" size="1">
                                                            <option value="1" selected>01</option>
                                                            <option value="2">02</option>
                                                            <option value="3">03</option>
                                                            <option value="4">04</option>
                                                            <option value="5">05</option>
                                                            <option value="6">06</option>
                                                            <option value="7">07</option>
                                                            <option value="8">08</option>
                                                            <option value="9">09</option>
                                                            <option value="10">10</option>
                                                            <option value="11">11</option>
                                                            <option value="12">12</option>
                                                            <option value="13">13</option>
                                                            <option value="14">14</option>
                                                            <option value="15">15</option>
                                                            <option value="16">16</option>
                                                            <option value="17">17</option>
                                                            <option value="18">18</option>
                                                            <option value="19">19</option>
                                                            <option value="20">20</option>
                                                            <option value="21">21</option>
                                                            <option value="22">22</option>
                                                            <option value="23">23</option>
                                                            <option value="24">24</option>
                                                            <option value="25">25</option>
                                                            <option value="26">26</option>
                                                            <option value="27">27</option>
                                                            <option value="28">28</option>
                                                            <option value="29">29</option>
                                                            <option value="30">30</option>
                                                            <option value="31">31</option>
                                                        </select>
                                                    </th>
                                                    <th>
                                                        <select name="EDyearSearch" id="eyearId" size="1" onchange="echange(this.form);">
                                                            <option value="2011" selected>2011</option>
                                                            <option value="2012">2012</option>
                                                            <option value="2013">2013</option>
                                                            <option value="2014">2014</option>
                                                            <option value="2015">2015</option>
                                                            <option value="2016">2016</option>
                                                            <option value="2017">2017</option>
                                                            <option value="2018">2018</option>
                                                            <option value="2019">2019</option>
                                                            <option value="2020">2020</option>
                                                            <option value="2021">2021</option>
                                                            <option value="2022">2022</option>
                                                        </select>
                                                        <select name="EDmonthSearch" id="emonthId" size="1" onchange="echange(this.form);">
                                                            <option value="1">January</option>
                                                            <option value="2">February</option>
                                                            <option value="3">March</option>
                                                            <option value="4">April</option>
                                                            <option value="5">May</option>
                                                            <option value="6">June</option>
                                                            <option value="7">July</option>
                                                            <option value="8">August</option>
                                                            <option value="9">September</option>
                                                            <option value="10">October</option>
                                                            <option value="11">November</option>
                                                            <option value="12">December</option>
                                                        </select>
                                                        <select name="EDdaySearch" id="edayId" size="1">
                                                            <option value="1" selected>01</option>
                                                            <option value="2">02</option>
                                                            <option value="3">03</option>
                                                            <option value="4">04</option>
                                                            <option value="5">05</option>
                                                            <option value="6">06</option>
                                                            <option value="7">07</option>
                                                            <option value="8">08</option>
                                                            <option value="9">09</option>
                                                            <option value="10">10</option>
                                                            <option value="11">11</option>
                                                            <option value="12">12</option>
                                                            <option value="13">13</option>
                                                            <option value="14">14</option>
                                                            <option value="15">15</option>
                                                            <option value="16">16</option>
                                                            <option value="17">17</option>
                                                            <option value="18">18</option>
                                                            <option value="19">19</option>
                                                            <option value="20">20</option>
                                                            <option value="21">21</option>
                                                            <option value="22">22</option>
                                                            <option value="23">23</option>
                                                            <option value="24">24</option>
                                                            <option value="25">25</option>
                                                            <option value="26">26</option>
                                                            <option value="27">27</option>
                                                            <option value="28">28</option>
                                                            <option value="29">29</option>
                                                            <option value="30">30</option>
                                                            <option value="31">31</option>
                                                        </select>
                                                    </th>
                                                    <th>
                                                        <select name="statusSearch">
                                                            <option>on-going</option>
                                                            <option>finish</option>
                                                        </select>
                                                    </th>
                                                </tr>
                                            </thead>
                                        </table>
                                        <input type="submit" value="SearchTask" name="action" />
                                    </form>
                                    <c:import url="xml/Tasks.xml" var="xmldoc2"/>
                                    <c:import url="xsl/Tasks.xsl" var="xsldoc2"/>
                                    <x:transform xml="${xmldoc2}" xslt="${xsldoc2}">
                                    </x:transform>
                                    <input align="middle" type="button" name="Update Tasks" value="Update Tasks" onclick="updateTasks();" />
                                    <form action="Controller" method="POST">
                                        <input align="middle" type="submit" name="action" value="ACTION_EXPORT_PDF" />
                                    </form>
                                </c:if>
                            </c:if>
                        </div>
                    </div>
                </div>
            </div>

            <div id="footer">

            </div>
        </div>
    </body>
</html>
